package com.blog.cmrpersonalblog.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;

/**
 * 邮箱验证码实体类
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("email_verification")
public class EmailVerification {

    /**
     * 验证码ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 邮箱地址
     */
    @TableField("email")
    private String email;

    /**
     * 验证码
     */
    @TableField("verification_code")
    private String verificationCode;

    /**
     * 验证类型（REGISTER-注册验证，RESET_PASSWORD-重置密码，CHANGE_EMAIL-更换邮箱）
     */
    @TableField("verification_type")
    private String verificationType;

    /**
     * 用户ID（注册时为空）
     */
    @TableField("user_id")
    private Long userId;

    /**
     * 是否已使用（0-未使用，1-已使用）
     */
    @TableField("is_used")
    private Integer isUsed;

    /**
     * 过期时间
     */
    @TableField("expire_time")
    private LocalDateTime expireTime;

    /**
     * 请求IP地址
     */
    @TableField("ip_address")
    private String ipAddress;

    /**
     * 用户代理
     */
    @TableField("user_agent")
    private String userAgent;

    /**
     * 创建时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    /**
     * 使用时间
     */
    @TableField("use_time")
    private LocalDateTime useTime;

    /**
     * 验证类型枚举
     */
    public enum VerificationType {
        REGISTER("REGISTER", "注册验证"),
        RESET_PASSWORD("RESET_PASSWORD", "重置密码"),
        CHANGE_EMAIL("CHANGE_EMAIL", "更换邮箱");

        private final String code;
        private final String description;

        VerificationType(String code, String description) {
            this.code = code;
            this.description = description;
        }

        public String getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static VerificationType fromCode(String code) {
            for (VerificationType type : values()) {
                if (type.getCode().equals(code)) {
                    return type;
                }
            }
            return null;
        }
    }
}
